<template>
  <el-dialog
    :title="!dataForm.magicTaskCode ? '创建任务' : '修改任务'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="任务类型" prop="magicTaskTypeEnum">
        <el-select v-model="dataForm.magicTaskTypeEnum" placeholder="请选择任务类型" clearable :disabled="taskTypeDisabled">
          <el-option
            v-for="item in availableTaskTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务主体类型" prop="magicTaskEntityTypeEnum">
        <el-select v-model="dataForm.magicTaskEntityTypeEnum" placeholder="请选择任务主体类型" clearable>
          <el-option label="用户" value="WMH_GALAXY_MAGIC_TASK_ENTITY_USER"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务主体值" prop="magicTaskEntityValue">
        <el-input v-model="dataForm.magicTaskEntityValue" placeholder="请输入任务主体值"></el-input>
      </el-form-item>
      <el-form-item label="任务事件类型" prop="magicTaskEventTypeEnum">
        <el-select v-model="dataForm.magicTaskEventTypeEnum" placeholder="请选择事件类型" clearable @change="handleEventTypeChange">
          <el-option
            v-for="item in filteredEventTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务事件值" prop="magicTaskEventValue">
        <el-input v-model="dataForm.magicTaskEventValue" placeholder="请输入任务事件值" type="textarea" :rows="4"></el-input>
      </el-form-item>
      <el-form-item label="周期" prop="magicTaskCycleEnum">
        <el-select v-model="dataForm.magicTaskCycleEnum" placeholder="请选择周期" clearable>
          <el-option label="自然15分钟" value="NATURAL_30_MINUTES"></el-option>
          <el-option label="自然30分钟" value="IMMEDIATE"></el-option>
          <el-option label="自然日" value="NATURAL_DAY"></el-option>
          <el-option label="自然小时" value="NATURAL_HOURS"></el-option>
          <el-option label="自然周" value="NATURAL_WEEK"></el-option>
          <el-option label="自然月" value="NATURAL_MONTH"></el-option>
          <el-option label="自然年" value="NATURAL_YEAR"></el-option>
          <el-option label="无周期" value="NATURAL_NULL"></el-option>
          <el-option label="永久" value="NATURAL_PERMANENT"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="扩展字段" prop="extInfo" >
        <el-input v-model="dataForm.extInfo" placeholder="请输入扩展字段"  type="textarea" :rows="4"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      taskTypeDisabled: false,
      dataForm: {
        creator: '',
        magicTaskTypeEnum:'',
        magicParentTaskCode: '',
        magicTaskEntityTypeEnum:'',
        magicTaskEntityValue:'',
        magicTaskEventTypeEnum:'',
        magicTaskEventValue: '',
        magicTaskCycleEnum:'',
        prority: 0,
        executeTime: '',
        extInfo: '',
        webSiteCode: '',
        env: ''
      },
      // 定义所有任务类型选项
      allTaskTypes: [
        { label: "生成图片", value: "WMH_GALAXY_MAGIC_TASK_IMAGE_GEN" },
        { label: "图片处理", value: "WMH_GALAXY_MAGIC_TASK_IMAGE_PROCESS" },
        { label: "全量同步", value: "WMH_GALAXY_MAGIC_TASK_ES_UPDATE" },
        { label: "增量同步", value: "WMH_GALAXY_MAGIC_TASK_ES_INCREMENTAL_UPDATE" },
        { label: "订单处理", value: "WMH_GALAXY_MAGIC_TASK_ORDER_PROCESS" }
      ],
      // 定义任务类型与事件类型的映射关系
      taskEventTypeMap: {
        'WMH_GALAXY_MAGIC_TASK_IMAGE_GEN': [
          { label: "文字生成图片", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_GEN_BY_TEXT" },
          { label: "图片生成图片", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_GEN_BY_IMAGE" },
          { label: "风格生成图片", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_GEN_BY_STYLE" },
          { label: "图文生成图片", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_GEN_BY_IMAGE_AND_TEXT" },
          { label: "千问生图", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_QWEN" },
          { label: "生成预览图", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_PREVIEW" }
        ],
        'WMH_GALAXY_MAGIC_TASK_IMAGE_PROCESS': [
          { label: "图像高清处理", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_HIGH_DEFINITION" },
          { label: "消除笔", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_DRAW" },
          { label: "扩图", value: "WMH_GALAXY_MAGIC_TASK_EVENT_IMAGE_EXPAND" }
        ],
        'WMH_GALAXY_MAGIC_TASK_ES_UPDATE': [
          { label: "素材全量同步", value: "WMH_GALAXY_MAGIC_TASK_EVENT_METERIAL_FULL_UPDATE" },
          { label: "模板全量同步", value: "WMH_GALAXY_MAGIC_TASK_EVENT_TEMPLATE_FULL_UPDATE" },
        ],
        'WMH_GALAXY_MAGIC_TASK_ES_INCREMENTAL_UPDATE' : [
          { label: "素材增量同步", value: "WMH_GALAXY_MAGIC_TASK_EVENT_METERIAL_INCREMENTAL_UPDATE" },
          { label: "模板增量同步", value: "WMH_GALAXY_MAGIC_TASK_EVENT_TEMPLATE_INCREMENTAL_UPDATE" },
        ],
        'WMH_GALAXY_MAGIC_TASK_ORDER_PROCESS': [
          { label: "订单处理", value: "WMH_GALAXY_MAGIC_TASK_EVENT_ORDER_PROCESS" }
        ]
      },
      dataRule: {
        magicTaskTypeEnum: [
          { required: true, message: '请选择任务类型', trigger: 'change' }
        ],
        magicTaskEntityTypeEnum: [
          { required: true, message: '请选择任务实体类型', trigger: 'change' }
        ],
        magicTaskEntityValue: [
          { required: true, message: '请输入任务主体值', trigger: 'blur' }
        ],
        magicTaskEventTypeEnum: [
          { required: true, message: '请选择事件类型', trigger: 'change' }
        ],
        magicTaskEventValue: [
          { required: true, message: '请输入当前值', trigger: 'blur' }
        ],
        magicTaskCycleEnum: [
          { required: true, message: '请选择周期', trigger: 'change' }
        ],
        prority: [
          { required: true, message: '请输入优先级', trigger: 'blur' }
        ],
        executeTime: [
          { required: true, message: '请输入调度时间', trigger: 'blur' }
        ],
        webSiteCode: [
          { required: true, message: '请输入站点', trigger: 'blur' }
        ],
        env: [
          { required: true, message: '请输入环境', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 根据选择的任务类型过滤事件类型选项
    filteredEventTypes() {
      if (this.dataForm.magicTaskTypeEnum) {
        return this.taskEventTypeMap[this.dataForm.magicTaskTypeEnum] || [];
      }
      // 如果没有选择任务类型，则显示所有事件类型
      let allEventTypes = [];
      for (let key in this.taskEventTypeMap) {
        allEventTypes = allEventTypes.concat(this.taskEventTypeMap[key]);
      }
      return allEventTypes;
    },
    // 可用的任务类型（当通过事件类型选中任务类型时，只显示对应的那一个）
    availableTaskTypes() {
      if (this.taskTypeDisabled) {
        // 当任务类型被禁用时，只显示当前选中的任务类型
        return this.allTaskTypes.filter(item => item.value === this.dataForm.magicTaskTypeEnum);
      } else {
        // 否则显示所有任务类型
        return this.allTaskTypes;
      }
    }
  },
  methods: {
    init(magicTaskCode) {
      this.dataForm.magicTaskCode = magicTaskCode || ''
      this.taskTypeDisabled = false
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.magicTaskCode) {
          this.$http({
            url: this.$http.adornUrl(`/galaxybase/galaxybase/queryWormholeGalaxyTask`),
            method: 'post',
            data: this.$http.adornData({
              "magicTaskCode": magicTaskCode
            })
          }).then(({ data }) => {
            if (data && data.success) {
              this.dataForm.creator = data.data.creator
              this.dataForm.magicTaskStatus = data.data.magicTaskStatus
              this.dataForm.magicTaskType = data.data.magicTaskType
              this.dataForm.magicTaskEntityType = data.data.magicTaskEntityType
              this.dataForm.magicTaskEntityValue = data.data.magicTaskEntityValue
              this.dataForm.magicTaskEventType = data.data.magicTaskEventType
              this.dataForm.magicTaskEventValue = data.data.magicTaskEventValue
              this.dataForm.magicTaskCycle = data.data.magicTaskCycle
              this.dataForm.prority = data.data.prority
              this.dataForm.executeTime = data.data.executeTime
              this.dataForm.extInfo = data.data.extInfo
              this.dataForm.webSiteCode = data.data.webSiteCode
              this.dataForm.env = data.data.env

              // 根据已有的事件类型反向设置任务类型
              this.setTaskTypeByEventType(data.data.magicTaskEventType);
            }
          })
        }
      })
    },
    // 根据事件类型反向设置任务类型
    setTaskTypeByEventType(eventType) {
      for (let taskType in this.taskEventTypeMap) {
        const eventTypes = this.taskEventTypeMap[taskType];
        if (eventTypes.some(item => item.value === eventType)) {
          this.dataForm.magicTaskTypeEnum = taskType;
          break;
        }
      }
    },
    // 处理事件类型变更
    handleEventTypeChange(value) {
      // 根据选择的事件类型反向设置任务类型
      if (value) {
        this.setTaskTypeByEventType(value);
        // 锁定任务类型选择框
        this.taskTypeDisabled = true;
      } else {
        // 如果清空了事件类型，则解锁任务类型选择框
        this.taskTypeDisabled = false;
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/galaxybase/magictask/${!this.dataForm.magicTaskCode ? 'createWormholeGalaxyMagicTask' : 'executeWormholeGalaxyMagicTask'}`),
            method: 'post',
            data: this.$http.adornData({
              'creator': this.dataForm.creator,
              'magicTaskTypeEnum': this.dataForm.magicTaskTypeEnum,
              'magicParentTaskCode': this.dataForm.magicParentTaskCode,
              'magicTaskEntityTypeEnum': this.dataForm.magicTaskEntityTypeEnum,
              'magicTaskEntityValue': this.dataForm.magicTaskEntityValue,
              'magicTaskEventTypeEnum': this.dataForm.magicTaskEventTypeEnum,
              'magicTaskEventValue': this.dataForm.magicTaskEventValue,
              'magicTaskCycleEnum': this.dataForm.magicTaskCycleEnum,
              'executeTime': this.dataForm.executeTime,
              'extInfo': this.dataForm.extInfo,
              'webSiteCode': this.dataForm.webSiteCode,
              'env': this.dataForm.env
            })
          }).then(({ data }) => {
            if (data && data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
